package com.kitty.game.fight.pet;

import static com.kitty.game.utils.StatusConst.STATUS_SHEN_SHE_QU_YI;

import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.fight.util.FightRateUtil;
import com.kitty.game.utils.StatusConst;

/**
 * 舍身取义
 * 对己方使用成功后，可以以自身死亡为代价在回合开始时恢复队友气血。技能等级越高恢复的气血越多
 */
public class SheShenQuYiProcessor extends PetSkillProcessor {

    @Override
    public void trigger(Fight fight, FightObject effector, FightObject target, int skillLevel, Object... args) {
        if (effector.isFallDown()) {
            return;
        }
        if (!fight.isSameSide(effector, target)) {
            return;
        }
        // 回血值：技能等级*0.01换算百分比
        int recoverHp = (int)(effector.getMaxLife() * skillLevel * 0.01);
        target.setStatusScore(StatusConst.STATUS_SHEN_SHE_QU_YI, recoverHp);

        FightMessageService fightMessageService = SpringUtils.getFightMessageService();

        // 施法者自杀
        effector.setCurrLife(0);
        FightMessageUtil.sendFightPackage(fight,
                fightMessageService.decreaseLife(effector.getId(), effector.getId(), recoverHp - effector.getCurrLife(), 0, 0));
        FightMessageUtil.sendFightPackage(fight, fightMessageService.updateTargetLife(effector.getId(), effector.getCurrLife()));

        int sheShenRecove = target.getStatusScore(STATUS_SHEN_SHE_QU_YI);
        if (sheShenRecove > 0) {
            if (target.isFallDown()) {// 复活动画
                FightMessageUtil.sendFightPackage(fight, fightMessageService.revive(target.getId()));
            }
            FightMessageUtil.sendFightPackage(fight,
                    fightMessageService.decreaseLife(effector.getId(), target.getId(), recoverHp, 4098, 10005));
            target.setCurrLife(target.getCurrLife() + sheShenRecove);
            FightMessageUtil.sendFightPackage(fight, fightMessageService.updateTargetLife(target.getId(), target.getCurrLife()));
            target.removeStatus(STATUS_SHEN_SHE_QU_YI);
        }
    }

    private boolean isRateHit(int skillLevel) {
        int rate = (int) (skillLevel * 30 * 0.7);
        return FightRateUtil.isHit(rate, 30);
    }

    @Override
    public Boolean effect(Fight fight, FightObject effector, FightObject target, Object... args) {

        return Boolean.TRUE;
    }

}
